﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text;
using T.FAS.Runtime.Metadata.Entity;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Metadata.Dao
{
    public class MetaDataQuoteUpdateDao : DaoService<MetaDataQuoteUpdate>
    {
        public MetaDataQuoteUpdateDao(string db = "TFAS") : base()
        {
            this.DBName = db;
        }

        public override string IDColumnName
        {
            get
            {
                return "ID";
            }
        }

        public override string TableName
        {
            get
            {
                return "Meta_QuoteUpdate";
            }
        }
        public override List<string> ExceptColumnName
        {
            get
            {
                return new List<string>()
                {
                    "QuoteMetaDataCode",
                    "QuoteMetaDataName",
                    "QuoteMetaTypeName",
                    "QuoteMetaDataVersion",
                    "MetaDataVersion"
                };
            }
        }
        public List<MetaDataQuoteUpdate> QueryDependencyMetaDataQuoteUpdates(string projectBranchID, string metaDataID)
        {
            var sqlCondition = @"quote.QuoteProjectBranchID=@QuoteProjectBranchID AND quote.UpdateState=0 AND quote.QuoteMetaDataID in ( select ParentMetaDataID from meta_dependency where MetaDataID=@MetaDataID and ProjectBranchID=@QuoteProjectBranchID)";
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>
            {
                this.DB.MakeInParam("@QuoteProjectBranchID", TDbDataType.VarChar, projectBranchID),
                this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metaDataID),
            };
            List<MetaDataQuoteUpdate> list = new List<MetaDataQuoteUpdate>();
            var ds = this.DB.ExecuteDataSet(string.Format(@"SELECT quote.ID, quote.QuoteMetaDataID, quote.QuoteMetaTypeID, quote.QuoteProjectBranchID, quote.QuoteMetaDataVersionID, quote.ProjectBranchID, quote.MetaDataVersionID, quote.UpdateState, quote.Creator, quote.CreateTime, quote.LastModifier, quote.LastModifyTime,meta.Code QuoteMetaDataCode,meta.Name QuoteMetaDataName,typeInfo.Name QuoteMetaTypeName,mv1.Version QuoteMetaDataVersion,mv2.Version MetaDataVersion FROM {0} quote
left join meta_basicinfo meta on meta.ID = quote.QuoteMetaDataID and meta.ProjectBranchID = quote.ProjectBranchID
left join meta_typeinfo typeInfo on typeInfo.ID = quote.QuoteMetaTypeID
left join meta_version mv1 on mv1.ID = quote.QuoteMetaDataVersionID and mv1.ProjectBranchID = quote.ProjectBranchID
left join meta_Version mv2 on mv2.ID = quote.MetaDataVersionID and mv2.ProjectBranchID = quote.ProjectBranchID WHERE {1}", TableName, sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var row = ds.Tables[0].Rows[i];
                    var quoteUpdate = this.ConvertToObject(this.GetColumns(), row);
                    quoteUpdate.QuoteMetaDataCode = Convert.ToString(row["QuoteMetaDataCode"]);
                    quoteUpdate.QuoteMetaDataName = Convert.ToString(row["QuoteMetaDataName"]);
                    quoteUpdate.QuoteMetaTypeName = Convert.ToString(row["QuoteMetaTypeName"]);
                    quoteUpdate.QuoteMetaDataVersion = Convert.ToString(row["QuoteMetaDataVersion"]);
                    quoteUpdate.MetaDataVersion = Convert.ToString(row["MetaDataVersion"]);
                    list.Add(quoteUpdate);
                }
            }
            return list;
        }
        public List<MetaDataQuoteUpdate> QueryUndoMetaDataQuoteUpdates(string projectBranchID)
        {
            var sqlCondition = "quote.QuoteProjectBranchID=@QuoteProjectBranchID AND quote.UpdateState in (0,-1)";
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>
            {
                this.DB.MakeInParam("@QuoteProjectBranchID", TDbDataType.VarChar, projectBranchID),
            };
            List<MetaDataQuoteUpdate> list = new List<MetaDataQuoteUpdate>();
            var ds = this.DB.ExecuteDataSet(string.Format(@"SELECT quote.ID, quote.QuoteMetaDataID, quote.QuoteMetaTypeID, quote.QuoteProjectBranchID, quote.QuoteMetaDataVersionID, quote.ProjectBranchID, quote.MetaDataVersionID, quote.UpdateState, quote.Creator, quote.CreateTime, quote.LastModifier, quote.LastModifyTime,meta.Code QuoteMetaDataCode,meta.Name QuoteMetaDataName,typeInfo.Name QuoteMetaTypeName,mv1.Version QuoteMetaDataVersion,mv2.Version MetaDataVersion FROM {0} quote
left join meta_basicinfo meta on meta.ID = quote.QuoteMetaDataID and meta.ProjectBranchID = quote.ProjectBranchID
left join meta_typeinfo typeInfo on typeInfo.ID = quote.QuoteMetaTypeID
left join meta_version mv1 on mv1.ID = quote.QuoteMetaDataVersionID and mv1.ProjectBranchID = quote.ProjectBranchID
left join meta_Version mv2 on mv2.ID = quote.MetaDataVersionID and mv2.ProjectBranchID = quote.ProjectBranchID WHERE {1}", TableName, sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var row = ds.Tables[0].Rows[i];
                    var quoteUpdate = this.ConvertToObject(this.GetColumns(), row);
                    quoteUpdate.QuoteMetaDataCode = Convert.ToString(row["QuoteMetaDataCode"]);
                    quoteUpdate.QuoteMetaDataName = Convert.ToString(row["QuoteMetaDataName"]);
                    quoteUpdate.QuoteMetaTypeName = Convert.ToString(row["QuoteMetaTypeName"]);
                    quoteUpdate.QuoteMetaDataVersion = Convert.ToString(row["QuoteMetaDataVersion"]);
                    quoteUpdate.MetaDataVersion = Convert.ToString(row["MetaDataVersion"]);
                    list.Add(quoteUpdate);
                }
            }
            return list;
        }
        public List<MetaDataQuoteUpdate> QueryMetaDataQuoteUpdates(string projectBranchID, int updateState)
        {
            var sqlCondition = "quote.QuoteProjectBranchID=@QuoteProjectBranchID AND quote.UpdateState=@UpdateState";
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>
            {
                this.DB.MakeInParam("@QuoteProjectBranchID", TDbDataType.VarChar, projectBranchID),
                this.DB.MakeInParam("@UpdateState", TDbDataType.Int, updateState),
            };
            List<MetaDataQuoteUpdate> list = new List<MetaDataQuoteUpdate>();
            var ds = this.DB.ExecuteDataSet(string.Format(@"SELECT quote.ID, quote.QuoteMetaDataID, quote.QuoteMetaTypeID, quote.QuoteProjectBranchID, quote.QuoteMetaDataVersionID, quote.ProjectBranchID, quote.MetaDataVersionID, quote.UpdateState, quote.Creator, quote.CreateTime, quote.LastModifier, quote.LastModifyTime,meta.Code QuoteMetaDataCode,meta.Name QuoteMetaDataName,typeInfo.Name QuoteMetaTypeName,mv1.Version QuoteMetaDataVersion,mv2.Version MetaDataVersion FROM {0} quote
left join meta_basicinfo meta on meta.ID = quote.QuoteMetaDataID and meta.ProjectBranchID = quote.ProjectBranchID
left join meta_typeinfo typeInfo on typeInfo.ID = quote.QuoteMetaTypeID
left join meta_version mv1 on mv1.ID = quote.QuoteMetaDataVersionID and mv1.ProjectBranchID = quote.ProjectBranchID
left join meta_Version mv2 on mv2.ID = quote.MetaDataVersionID and mv2.ProjectBranchID = quote.ProjectBranchID WHERE {1}", TableName, sqlCondition),paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var row = ds.Tables[0].Rows[i];
                    var quoteUpdate = this.ConvertToObject(this.GetColumns(), row);
                    quoteUpdate.QuoteMetaDataCode = Convert.ToString(row["QuoteMetaDataCode"]);
                    quoteUpdate.QuoteMetaDataName = Convert.ToString(row["QuoteMetaDataName"]);
                    quoteUpdate.QuoteMetaTypeName = Convert.ToString(row["QuoteMetaTypeName"]);
                    quoteUpdate.QuoteMetaDataVersion = Convert.ToString(row["QuoteMetaDataVersion"]);
                    quoteUpdate.MetaDataVersion = Convert.ToString(row["MetaDataVersion"]);
                    list.Add(quoteUpdate);
                }
            }
            return list;
        }
        public MetaDataQuoteUpdate GetMetaDataQuoteUpdate(string projectBranchID, string metaDataID, int updateState)
        {
            var sqlCondition = "QuoteProjectBranchID=@QuoteProjectBranchID AND UpdateState=@UpdateState";
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>
            {
                this.DB.MakeInParam("@QuoteProjectBranchID", TDbDataType.VarChar, projectBranchID),
                this.DB.MakeInParam("@UpdateState", TDbDataType.Int, updateState),
            };
            if (string.IsNullOrEmpty(metaDataID) == false) {
                sqlCondition = string.Format("{0} AND QuoteMetaDataID=@QuoteMetaDataID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@QuoteMetaDataID", TDbDataType.VarChar, metaDataID));
            }
            MetaDataQuoteUpdate  data = null;
            var ds = this.DB.ExecuteDataSet(string.Format(@"SELECT ID, QuoteMetaDataID, QuoteMetaTypeID, QuoteProjectBranchID, QuoteMetaDataVersionID, ProjectBranchID, MetaDataVersionID, UpdateState, Creator, CreateTime, LastModifier, LastModifyTime FROM {0} WHERE {1}", TableName, sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                data = this.ConvertToObject(this.GetColumns(), ds.Tables[0].Rows[0]);                
            }
            return data;
        }

        public void DeleteQuoteUpdate(string quoteMetaDataID, string projectBranchID, string quoteProjectBranchID = "")
        {
            var sqlCondition = "QuoteMetaDataID=@QuoteMetaDataID";
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>
            {
                this.DB.MakeInParam("@QuoteMetaDataID", TDbDataType.VarChar, quoteMetaDataID),
            };
            if (string.IsNullOrEmpty(projectBranchID) == false)
            {
                sqlCondition = string.Format("{0} AND ProjectBranchID=@ProjectBranchID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchID));
            }
            if (string.IsNullOrEmpty(quoteProjectBranchID) == false) {
                sqlCondition = string.Format("{0} AND QuoteProjectBranchID=@QuoteProjectBranchID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@QuoteProjectBranchID", TDbDataType.VarChar, quoteProjectBranchID));
            }
            this.DB.ExecSqlStatement(string.Format("DELETE FROM {0} WHERE {1}", TableName, sqlCondition), paramsList.ToArray());
        }
        public void UpdateQuoteUpdate(string projectBranchID, string metaDataID, string modifier, int updateState) {
            var sqlCondition = "QuoteProjectBranchID=@QuoteProjectBranchID AND QuoteMetaDataID=@QuoteMetaDataID";
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>
            {
                this.DB.MakeInParam("@QuoteProjectBranchID", TDbDataType.VarChar, projectBranchID),
                this.DB.MakeInParam("@QuoteMetaDataID", TDbDataType.VarChar, metaDataID),
                this.DB.MakeInParam("@LastModifier", TDbDataType.VarChar, modifier),
                this.DB.MakeInParam("@LastModifyTime", TDbDataType.DateTime, DateTime.Now),
                this.DB.MakeInParam("@UpdateState", TDbDataType.Int, updateState)
            };
            var ds = this.DB.ExecSqlStatement(string.Format("Update {0} SET UpdateState=@UpdateState,LastModifier=@LastModifier,LastModifyTime=@LastModifyTime WHERE {1}", TableName, sqlCondition), paramsList.ToArray());            

        }
    }
}
